package com.googlecode.animator.controller
{
	import com.googlecode.animator.data.viewports.ViewportVO;
	
	import flash.utils.getDefinitionByName;
	
	import mx.core.Application;
	
	import org.puremvc.as3.interfaces.INotification;
	
	/**
	 * Adds cameras to the model. 
	 * @author USER
	 * 
	 */
	public class AddDefaultViewportsCommand extends BaseCommand
	{
		/**
		 * Executes command. 
		 * @param notification
		 * 
		 */
		public override function execute(notification:INotification):void
		{
			

			var defaultXML:XML = notification.getBody() as XML;
			var i:uint;
			
			for each(var viewport:XML in defaultXML.layout.viewports.viewport)
			{
				i=0;
				
				for each(var component:XML in viewport.component)
				{
					var componentDef:Class = getDefinitionByName(component.definition) as Class;
					var child:* = new componentDef();
				
					for each(var param:XML in component.param)
					{
						child[param.@name] = param;
					}

					var mediatorDef:Class = getDefinitionByName(component.mediator) as Class;
	
					(Application.application as Revised3DAnimator)[viewport.@id].addChild(child);
					facade.registerMediator(new mediatorDef((Application.application as Revised3DAnimator)[viewport.@id].getChildAt(i)));

					i++; 
				}
				
				var viewportVO:ViewportVO = new ViewportVO();
				
				viewportVO.viewportID = viewport.@id;
				viewportVO.currentCamera = viewport..component.param.(@name == "currentCamera");	

				settings.addViewport(viewportVO);
				//sendNotification(Notifications.ADD_VIEWPORT, viewportVO);
			}
		}
		
	}
}